using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BigFishSkillSprinting : MonoBehaviour
{
    public Transform playertransform;
    public EnemyBasic player;
    public EnemyAttack playerattack;
    private Rigidbody2D myRigidbody;
    public float damageratio;
    private TrailRenderer trail;
    public float skilltime;
    public float preparetime;
    public int face;
    public float runspeedratio;
    private bool IsSkilling;
    private BoxCollider2D collider;

    public float StiffTime;
    public float RepelDistance;
    public int RepelTimes;
    public float CD;
    private float CDtime;
    public bool skillend;
    GameObject canvas;
    // Start is called before the first frame update
    public void Start()
    {
        playertransform = GetComponentInParent<Transform>();
        playerattack = transform.parent.GetComponentInChildren<EnemyAttack>();
        myRigidbody = GetComponentInParent<Rigidbody2D>();
        trail = GetComponentInChildren<TrailRenderer>();
        player = GetComponentInParent<EnemyBasic>();
        collider = GetComponentInParent<BoxCollider2D>();
        trail.enabled = false;
        IsSkilling = false;
        collider.enabled = false;
        skillend = false;
        CDtime = CD;
        canvas = transform.GetChild(1).gameObject;
        canvas.SetActive(false);
    }

    // Update is called once per frame
    void skillingend()
    {
        player.moveable = true;
        trail.enabled = false;
        myRigidbody.velocity = new Vector2(0, myRigidbody.velocity.y);
        IsSkilling = false;
        collider.enabled = false;
        CDtime = CD;
    }
    public IEnumerator Skilling()
    {
        yield return new WaitForSeconds(skilltime);
        if (IsSkilling) skillingend();

    }
    public IEnumerator preparing()
    {
        player.moveable = false;
        yield return new WaitForSeconds(preparetime);
        
        trail.enabled = true;
        IsSkilling = true;
        collider.enabled = true;
        canvas.SetActive(false);
        StartCoroutine(Skilling());

    }
    public void Skill()
    {
        canvas.SetActive(true);
        StartCoroutine(preparing());
    }
    public void OnTriggerEnter2D(Collider2D other)
    {
        if (other.gameObject.CompareTag("Player"))
        {

            other.GetComponent<PlayerBasic>().TakeDamage(damageratio * playerattack.damage, face, RepelTimes, RepelDistance, StiffTime);
        }
        if (other.gameObject.CompareTag("Creation"))
        {
            other.GetComponent<Creation>().TakeDamage(damageratio * playerattack.damage);
        }
        if (other.gameObject.CompareTag("Air"))
        {
            myRigidbody.velocity = Vector2.zero;
            if (IsSkilling) skillingend();
        }
    }
    public void Update()
    {
        face = playerattack.face;
        if (CDtime > 0)
        {
            CDtime -= Time.deltaTime;
        }
        if (Time.timeScale > 0)
        {
            if (CDtime <= 0)
            {
                Skill();
                CDtime = CD;
            }
        }
        if (IsSkilling)
        {
            myRigidbody.velocity = new Vector2(-face * runspeedratio * player.Runspeed, 0);
        }
    }
}
